package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.model.MessageDto;
import com.b2b.model.SystemRoleDto;
import com.b2b.model.UserDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Message;
import com.b2b.model.receive.MyMessage;
import com.b2b.service.MessageService;
import com.b2b.util.MessageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/message")
@PropertySource("classpath:config.properties")
public class MessageController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final MessageUtil messageUtil;

    @Autowired
    public MessageController(MessageUtil messageUtil){
        this.messageUtil = messageUtil;
    }

    @PostMapping("/add")
    public ResponseBean addMessage(@RequestBody MessageDto messageDto){
        if (messageDto.getSenderId() == messageDto.getReceiverId()){
            throw new CustomException("Id重复(Insert Failure)");
        }
        int count = messageUtil.insertMessage(messageDto);
        if (count <= 0){
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", messageUtil.getMessage(messageDto));
    }

    @GetMapping("/getDetail")
    public ResponseBean getMessage(MessageDto messageDto){
//        BaseDto baseDto = new BaseDto();
        Integer senderId = messageDto.getSenderId();
        Integer receiverId = messageDto.getReceiverId();
        int page = messageDto.getPage();
        int rows = messageDto.getRows();
        if (messageDto.getSidx() == null){
            PageHelper.startPage(page,rows);
        }else {
            PageHelper.startPage(page,rows);
            PageHelper.orderBy(messageDto.getSidx()+" "+messageDto.getSord());
        }
        List<Message> messages = messageUtil.getMessages(senderId,receiverId);
        PageInfo<Message> selectPage = new PageInfo<Message>(messages);
        if (messages ==null || messages.size() == 0){
            throw new CustomException("无数据(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/getMy")
    public ResponseBean getAllMessage(@RequestParam Integer companyId){
        List<MyMessage> messageList = messageUtil.selectMyMessage(companyId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", messageList);
    }
}
